import csv
import CParameter
import Explicit


def make_header(num: int, met: int) -> list:
    """
    制作表头
    :param num: 节点个数
    :param met: 采用方法
    :return: 表头
    """
    header = ['Time']
    if met == 1 or met == 21:
        for j in range(num):
            temp = 'Temp' + str(j + 1)
            header.append(temp)
    else:
        header.append('Iteration Step')
        for j in range(num):
            temp = 'Temp' + str(j + 1)
            header.append(temp)
            temp = 'Residual' + str(j + 1)
            header.append(temp)
    return header


if __name__ == '__main__':
    node = 50  # 离散节点个数
    node_length = CParameter.d / (2 * node)  # 每个节点的长度
    time_step = 0.1  # 时间步长
    total_step = 200  # 总共计算步数
    residual = 0.01  # 残差 在显式和隐式TDMA中没用
    max_iteration_step = 50  # 最大迭代步数

    '''
    1 时间显式
    21 时间隐式 TDMA
    22 时间隐式 FP
    23 时间隐式 GS
    24 时间隐式 ADI
    '''
    method = 1

    if method == 1:
        file = open('result_explicit.csv', 'w', newline='')
        writer = csv.writer(file)
        writer.writerow(make_header(node, method))  # 写表头

        data = [CParameter.t0] * node
        for a in range(total_step):
            time = [time_step * (a + 1)]
            new_data = Explicit.explicit(data, node_length, time_step)
            data = new_data
            writer.writerow(time + new_data)

        file.close()

    else:
        print('There is NO match method!')